package dao;

import java.io.Serializable;
import java.util.List;

import modelo.Bebida;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class BebidaDao implements Serializable{

		private static final long serialVersionUID = -1751632032341553241L;
	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	public BebidaDao() {
		super();
	}

	public BebidaDao(Session session, SessionFactory sessionFactory) {
		super();
		this.session = session;
	}

	public List<Bebida> obtenerBebida() {
		session=sessionFactory.openSession();
		
		Query query = session.createQuery("SELECT b FROM Bebida b");
		List<Bebida> b = query.list();
		
		session.close();
		
		return b;
	}
	
	public Bebida obtenerBebidaById(int id){
		session=sessionFactory.openSession();
		Query query = session.createQuery("from Bebida where IdBebida = :id ");
        query.setParameter("id", id);

        List<?> list = query.list();

        Bebida b = (Bebida)list.get(0);
        return b;
	}
	
	public void guardarBebida(Bebida b){
		session=sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(b);
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarBebida(Bebida b) {
		session=sessionFactory.openSession();
		session.beginTransaction();
		session.delete(b);
		session.getTransaction().commit();
		session.close();
	}


}

